<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
    /**
    * Definition for a binary tree node.
    * function TreeNode(val) {
    *     this.val = val;
    *     this.left = this.right = null;
    * }
    */
    /**
    * @param {TreeNode} root
    * @param {TreeNode} p
    * @param {TreeNode} q
    * @return {TreeNode}
    */
    var lowestCommonAncestor = function(root, p, q) {
        if(!root&&root==p&&root==q){
            return root;
        }
        var l=lowestCommonAncestor(root.left,p,q);
        var r=lowestCommonAncestor(root.right,p,q);
        if(!l&&!r)
        {
            return null;
        }
        else if(l&&!r)
        {
            return l;
        }else if(!l&&r){
            return r;
        }
        return root;
    };
    </script>
</body>
</html>